package com.szl.group.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

public class TransformUtil {

    /**
     * List转变（必须有无参构造器）
     */
    public static <O, N> List<N> transformList(List<O> oldList, Class<N> nClass) {
        if (CollectionUtils.isEmpty(oldList)) {
            return new ArrayList<>(0);
        }
        List<N> newList = new ArrayList<>(oldList.size());
        for (O old : oldList) {
            if (old == null) {
                continue;
            }
            N n = transformObject(old, nClass);
            if (n != null) {
                BeanUtils.copyProperties(old, n);
                newList.add(n);
            }
        }
        return newList;
    }

    /**
     * 对象转变（必须有无参构造器）
     */
    public static <O, N> N transformObject(O o, Class<N> nClass) {
        if (o == null) {
            return null;
        }
        N n = null;
        try {
            n = nClass.newInstance();
        } catch (Exception e) {
            //ignore
        }
        if (n != null) {
            BeanUtils.copyProperties(o, n);
        }
        return n;
    }

/*    *//**
     * result转变（必须有无参构造器）
     *//*
    public static <O, N> Result<N> transformResult(Result<O> mResult, Class<N> nClass) {
        Result<N> nResult = new Result<N>();
        nResult.setMsg(mResult.getMsg());
        nResult.setCode(mResult.getCode());
        O m = mResult.getData();
        if (m != null) {
            N n = null;
            try {
                n = nClass.newInstance();
            } catch (Exception e) {
                //ignore
            }
            if (n != null) {
                BeanUtils.copyProperties(m, n);
                nResult.setData(n);
            }
        }
        return nResult;
    }

    *//**
     * resultList转变
     *//*
    public static <O, N> Result<List<N>> transformResultList(Result<List<O>> result, Class<N> nClass) {
        Result<List<N>> newResult = new Result<>();
        newResult.setCode(result.getCode());
        newResult.setMsg(result.getMsg());
        List<O> oList = result.getData();
        newResult.setData(transformList(oList, nClass));
        return newResult;
    }*/


    /**
     * 使用零拷贝的方式复制文件
     * @param sourcePath 资源路径
     * @param targetPath 目标路径
     */
    public static void copyFileWithChannel(String sourcePath, String targetPath) {
        try {
            RandomAccessFile source = new RandomAccessFile(sourcePath, "r");
            FileChannel sourceChannel = source.getChannel();
            RandomAccessFile target = new RandomAccessFile(targetPath, "rw");
            FileChannel targetChannel = target.getChannel();
            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
